<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="值班标题" prop="title">
				<el-input v-model="form.title" placeholder="值班标题" maxlength="80" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="值班类型" prop="sortId">
						<el-select v-model="form.sortId" clearable style="width: 100%">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" maxlength="80" clearable></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="datetime" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="值班人" prop="accountId">
						<select-user-dialog v-model:accountId="form.accountId" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="tel">
						<el-input v-model="form.tel" placeholder="联系电话" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<msg-box-check-box v-model:msgType="form.msgType" code="duty"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 认</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
export default {
	emits: ['success', 'closed'],
	components: {
		msgBoxCheckBox,
		selectUserDialog
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '发布值班',
				edit: '编辑值班',
			},
			form: {
				recordId:'',
				title: '',
				sortId: '',
				sortNo: 0,
				beginTime: '',
				endTime: '',
				accountId: '',
				tel:'',
				msgType:''
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				title: [
					{required: true, message: '值班标题不能为空', trigger: 'blur'}
				],
				accountId: [
					{required: true, message: '值班人不能为空', trigger: 'blur'}
				],
				tel: [
					{required: true, message: '联系电话不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getDutySortListForSelect();
	},
	methods: {
		async getDutySortListForSelect()
		{
			var res = await this.$API.office.duty.getDutySortList.get();
			if (res.code == 200) {

				for(let i=0;i<res.data.length;i++)
				{
					let v={
						label:res.data[i].sortName,
						value:res.data[i].sortId
					}
					this.options.push(v)
				}
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			if (this.mode == "add") {
				this.btName = "发布";
			} else {
				this.btName = "保存";
			}
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.duty.insertDutyRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.duty.updateDutyRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.sortId = data.sortId
			this.form.sortNo = data.sortNo
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.accountId = data.accountId
			this.form.tel = data.tel
			this.form.msgType = data.msgType
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
